"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : Compatibility
Case Name   : 表含主键，导入数据指定参数replace
Create At   : 2023/05/29
Owner       : n@ningyali
Description :
        1.建表
        2.向表中导入数据，部分主键列重复
        3.向表中导入主键不重复的数据
        4.再次向表中导入主键不重复的数据
Expect      :
        1.创建成功
        2.成功
        3.成功
        4.成功
History     :
"""
import os
import unittest

from yat.test import Node

from testcase.COMPATIBILITY.GRAMMAR.LOAD. \
    Opengauss_Compatibility_Grammar_Load_Common import LoadData
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class LoadData0001(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.common = Common()
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_user = Node('PrimaryDbUser')
        self.db_name = 'db_load0001'
        self.tb_name = 'tb_load0001'
        self.file_path = os.path.join('/home', self.pri_user.ssh_user)
        self.load_data = LoadData()

        text1 = '----前置步骤1:创建数据文件; expect:创建成功----'
        self.log.info(text1)
        self.data1 = "#1,'aaa',17,13313692580;" \
                     "#2,'bbb',18,12213691470;" \
                     "#1,'ccc',19,13525801560;" \
                     "#2,'ddd',18,12213691470;" \
                     "#5,'eee',19,13525801560;"
        self.data2 = "#1,'aaa',17,13313692580;" \
                     "#2,'bbb',18,12213691470;" \
                     "#3,'ccc',19,13525801560;" \
                     "#4,'ddd',18,12213691470;" \
                     "#5,'eee',19,13525801560;"
        self.data3 = "#6,'xiaoming',18,12213691470;" \
                     "#7,'小名',19,13525801560;" \
                     "#8,'ddd',18,12213691470;" \
                     "#9,'eee',19,13525801560;"
        res = self.load_data.create_data_file(self.pri_user, 'load_data0001',
                                              self.data1, self.data2,
                                              self.data3)
        self.assertTrue(res, '执行失败:' + text1)

        text2 = '----前置步骤2:创建兼容B库; expect:创建成功----'
        self.log.info(text2)
        res = self.load_data.create_b_db(self.pri_sh, self.db_name)
        self.assertTrue(res, '执行失败:' + text2)

    def test_load(self):
        text = '----step1：建表 expect：建表成功----'
        self.log.info(text)
        cols = 'c_id int primary key,c_name char(20),' \
               'c_age int,c_num char(11)'
        res = self.load_data.create_table(self.pri_sh, self.db_name,
                                          self.tb_name, cols)
        self.assertTrue(res, '执行失败:' + text)

        text = '----step2：向表中导入数据，部分主键列重复; expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, 'load_data0001_1.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.tb_name, f_path)
        self.assertIn('3 rows', res1, '执行失败' + text)
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['1', 'ccc', '19', '13525801560'],
                  ['2', 'ddd', '18', '12213691470'],
                  ['5', 'eee', '19', '13525801560']]
        self.assertEqual(rows, expect, '执行失败:' + text)

        text = '----step3：向表中导入主键不重复的数据; expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, 'load_data0001_2.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.tb_name, f_path)
        self.assertIn('5 rows', res1, '执行失败' + text)
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['1', 'aaa', '17', '13313692580'],
                  ['2', 'bbb', '18', '12213691470'],
                  ['3', 'ccc', '19', '13525801560'],
                  ['4', 'ddd', '18', '12213691470'],
                  ['5', 'eee', '19', '13525801560']]
        self.assertEqual(rows, expect, '执行失败:' + text)

        text = '----step4：再次向表中导入主键不重复的数据；expect：成功----'
        self.log.info(text)
        f_path = os.path.join(self.file_path, 'load_data0001_3.txt')
        res1 = self.load_data.load_data(self.pri_sh, self.db_name,
                                        self.tb_name, f_path)
        self.assertIn('4 rows', res1, '执行失败' + text)
        rows, _ = self.common.parse_sql_query_res(res1)
        expect = [['6', 'xiaoming', '18', '12213691470'],
                  ['7', '小名', '19', '13525801560'],
                  ['8', 'ddd', '18', '12213691470'],
                  ['9', 'eee', '19', '13525801560']]
        self.assertEqual(rows, expect, '执行失败:' + text)

    def tearDown(self):
        self.log.info('----清理环境----')
        res = self.load_data.clear_env(self.pri_sh, self.pri_user,
                                       self.db_name,
                                       os.path.join(self.file_path,
                                                    "load_data0001*"))
        self.assertTrue(res, '执行失败:清理环境')
        self.log.info(f'----{os.path.basename(__file__)} end----')
